package top.gbiot.common.domain;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.math.BigDecimal;
import java.sql.Clob;
import java.util.*;

public class DataMap implements Map<String, Object>, Serializable {
    private static final long serialVersionUID = 1L;
    private Map<String, Object> map;

    public static DataMap New() {
        return new DataMap();
    }

    public static DataMap init() {
        return new DataMap();
    }

    public DataMap() {
        this.map = new HashMap();
    }

    public DataMap(Map<String, Object> map) {
        if (map != null) {
            try {
                Class<? extends Map> cls = map instanceof DataMap ? ((DataMap)map).map.getClass() : map.getClass();
                this.map = (Map)cls.newInstance();
            } catch (Exception var3) {
                throw new RuntimeException(var3);
            }

            this.map.putAll(map);
        } else {
            this.map = new HashMap();
        }

    }

    public DataMap(Class<? extends Map> cls) {
        try {
            this.map = (Map)cls.newInstance();
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    public DataMap(int initialCapacity) {
        this.map = new HashMap(initialCapacity);
    }

    public DataMap(String values) {
        this();
        if (StringUtils.isNotBlank(values)) {
            String[] var2 = values.split(",");
            int var3 = var2.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String val = var2[var4];
                String[] valArr = val.split(":");
                this.put((String)valArr[0], valArr[1]);
            }
        }

    }

    public DataMap put(String column, Object value) {
        this.map.put(column, value);
        return this;
    }

    public DataMap remove(Object column) {
        this.map.remove(column);
        return this;
    }

    public DataMap format(String column, String pattern) {
        String value = this.getStr(column);
        if (value != null) {
            pattern = pattern.replaceAll(":" + column, value);
            this.put((String)column, pattern);
        }

        return this;
    }

    public <T> T get(String column) {
        Object value = this.map.get(column);
        return (T) value;
    }

    public <T> T get(String column, Object defaultValue) {
        Object result = this.get(column);
        return result != null ? (T)result : (T)defaultValue;
    }

    public String getStr(String column) {
        return this.getStr(column, (String)null);
    }

    public String getStr(String column, String defaultValue) {
        Object value = this.get(column, defaultValue);
        return value != null ? value.toString() : null;
    }

    public Integer getInt(String column, Integer defaultValue) {
        Integer value = defaultValue;
        Object tmp = this.get(column);
        if (tmp != null) {
            if (tmp instanceof BigDecimal) {
                value = ((BigDecimal)tmp).intValue();
            } else if (tmp instanceof Integer) {
                value = (Integer)tmp;
            } else {
                String tmpStr = tmp.toString();
                if ("".equals(tmpStr)) {
                    value = null;
                } else {
                    value = Integer.valueOf(tmpStr);
                }
            }
        }

        return value;
    }

    public Integer getInt(String column) {
        return this.getInt(column, (Integer)null);
    }

    public <T> List<T> getList(String column) {
        return (List)this.get(column);
    }

    public <T> List<T> getList(String column, List<T> clazz) {
        List<T> list = this.getList(column);
        if (list == null) {
            list = clazz;
        }

        return list;
    }

    public <T> List<T> getCacheList(String column) {
        List<T> value = this.getList(column);
        if (value == null) {
            value = new ArrayList();
            this.put(column, value);
        }

        return (List)value;
    }

    public <T> Set<T> getSet(String column) {
        return (Set)this.get(column);
    }

    public <T> Set<T> getSet(String column, Set<T> clazz) {
        Set<T> list = this.getSet(column);
        if (list == null) {
            list = clazz;
        }

        return list;
    }

    public <T> Set<T> getCacheSet(String column) {
        Set<T> value = this.getSet(column);
        if (value == null) {
            value = new HashSet();
            this.put(column, value);
        }

        return (Set)value;
    }

    public Long getLong(String column) {
        return this.getLong(column, (Long)null);
    }

    public Long getLong(String column, Long defaultValue) {
        Long value = defaultValue;
        Object tmp = this.get(column);
        if (tmp != null) {
            if (tmp instanceof BigDecimal) {
                value = ((BigDecimal)tmp).longValue();
            } else if (tmp instanceof Long) {
                value = (Long)tmp;
            } else {
                String tmpStr = tmp.toString();
                if ("".equals(tmpStr)) {
                    value = null;
                } else {
                    value = Long.valueOf(tmpStr);
                }
            }
        }

        return value;
    }

    public Short getShort(String column) {
        return this.getShort(column, (Short)null);
    }

    public Short getShort(String column, Short defaultValue) {
        Short value = defaultValue;
        Object tmp = this.get(column);
        if (tmp != null) {
            if (tmp instanceof BigDecimal) {
                value = ((BigDecimal)tmp).shortValue();
            } else if (tmp instanceof Short) {
                value = (Short)tmp;
            } else {
                String tmpStr = tmp.toString();
                if ("".equals(tmpStr)) {
                    value = null;
                } else {
                    value = Short.valueOf(tmpStr);
                }
            }
        }

        return value;
    }

//    public Date getDate(String column) {
//        return this.getDate(column, (Date)null);
//    }
//
//    public Date getDate(String column, Date defaultValue) {
//        Object value = this.get(column, defaultValue);
//        return value != null && value instanceof String ? DateTimeUtil.str2DateTime(value.toString(), FrameParam.DEFAULT_DATE_MASK) : (Date)this.get(column, defaultValue);
//    }
//  TODO 暂未实现
 /*   public String getDateStr(String column, String mask) {
        Date value = (Date)this.get(column);
        return value != null ? DateTimeUtil.formatterDate(value, mask) : null;
    }*/

    public Double getDouble(String column) {
        return this.getDouble(column, (Double)null);
    }

    public Double getDouble(String column, Double defaultValue) {
        Double value = defaultValue;
        Object tmp = this.get(column);
        if (tmp != null) {
            if (tmp instanceof BigDecimal) {
                value = ((BigDecimal)tmp).doubleValue();
            } else if (tmp instanceof Double) {
                value = (Double)tmp;
            } else {
                String tmpStr = tmp.toString();
                if ("".equals(tmpStr)) {
                    value = null;
                } else {
                    value = Double.valueOf(tmpStr);
                }
            }
        }

        return value;
    }

    public BigDecimal getBigDecimal(String key) {
        return this.getBigDecimal(key, (BigDecimal)null);
    }

    public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
        Object value = this.get(key);
        if (value != null) {
            if (value instanceof BigDecimal) {
                return (BigDecimal)value;
            } else if (value instanceof Integer) {
                return new BigDecimal((Integer)value);
            } else {
                return value instanceof Double ? new BigDecimal((Double)value) : new BigDecimal(value.toString());
            }
        } else {
            return defaultValue;
        }
    }

    public Float getFloat(String column) {
        return this.getFloat(column, (Float)null);
    }

    public Float getFloat(String column, Float defaultValue) {
        Float value = defaultValue;
        Object tmp = this.get(column);
        if (tmp != null) {
            if (tmp instanceof BigDecimal) {
                value = ((BigDecimal)tmp).floatValue();
            } else if (tmp instanceof Float) {
                value = (Float)tmp;
            } else {
                value = Float.valueOf(tmp.toString());
            }
        }

        return value;
    }

    public Boolean getBoolean(String column) {
        return this.getBoolean(column, (Boolean)null);
    }

    public Boolean getBoolean(String column, Boolean defaultValue) {
        return (Boolean)this.get(column, defaultValue);
    }

    public String getClob(String column) {
        String reString = null;
        Clob clob = (Clob)this.get(column);
        if (clob != null) {
            Reader is = null;
            BufferedReader br = null;

            try {
                is = clob.getCharacterStream();
                br = new BufferedReader(is);
                String s = br.readLine();

                StringBuffer sb;
                for(sb = new StringBuffer(); s != null; s = br.readLine()) {
                    sb.append(s);
                }

                reString = sb.toString();
            } catch (Exception var11) {
                var11.printStackTrace();
            } finally {
                //IOUtils.closeQuietly(is);
                //IOUtils.closeQuietly(br);
                try {
                    is.close();
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

        return reString;
    }

    public DataMap getDo(String column) {
        return (DataMap)this.get(column);
    }

    public DataMap getCacheDo(String column) {
        DataMap value = this.getDo(column);
        if (value == null) {
            value = new DataMap();
            this.put((String)column, value);
        }

        return value;
    }

    public DataMap getDo(String column, DataMap defaultValue) {
        DataMap value = this.getDo(column);
        if (value == null) {
            value = defaultValue;
        }

        return value;
    }

    public File getFile(String column) {
        return (File)this.get(column);
    }

    public int size() {
        return this.map.size();
    }

    public boolean isEmpty() {
        return this.map.isEmpty();
    }

    public boolean containsKey(Object key) {
        return this.map.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return this.map.containsValue(value);
    }

    public Object get(Object key) {
        return this.map.get(key);
    }

    public void putAll(Map<? extends String, ? extends Object> m) {
        this.map.putAll(m);
    }

    public void clear() {
        this.map.clear();
    }

    public Set<String> keySet() {
        return this.map.keySet();
    }

    public Collection<Object> values() {
        return this.map.values();
    }

    public Set<Entry<String, Object>> entrySet() {
        return this.map.entrySet();
    }

    public String toString() {
        return this.map.toString();
    }

    public boolean equals(Object obj) {
        return this.map.equals(obj);
    }

    public int hashCode() {
        return this.map.hashCode();
    }
}
